package com.huawei.petSystemdemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.huawei.petSystemdemo.R;
import com.huawei.petSystemdemo.bean.Order;
import com.huawei.petSystemdemo.bean.Pets;
import com.huawei.petSystemdemo.bean.Shop;
import com.huawei.petSystemdemo.data.OrderRepository;
import com.huawei.petSystemdemo.data.ShopRepository;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class OrderShopAdapter extends RecyclerView.Adapter<OrderShopAdapter.OrderShopViewHolder> {

    private List<List<Pets>> petsList;
    private Context context;
    private int state;
    private List<Integer> shopId;

    public OrderShopAdapter(List<List<Pets>> pets, Context context,  List<Integer> shopId, int state) {
        this.petsList = pets;
        this.context = context;
        this.state = state;
        this.shopId = shopId;
    }

    public class OrderShopViewHolder extends RecyclerView.ViewHolder {

        TextView order_shop_name;
        TextView order_state;
        TextView order_add;
        TextView order_sure;
        RecyclerView order_pet_recycle;

        public OrderShopViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            order_shop_name = itemView.findViewById(R.id.order_shop_name);
            order_state = itemView.findViewById(R.id.order_state);
            order_add = itemView.findViewById(R.id.order_add);
            order_sure = itemView.findViewById(R.id.order_sure);
            order_pet_recycle = itemView.findViewById(R.id.order_pet_recycle);

            LinearLayoutManager manager = new LinearLayoutManager(context);
            manager.setOrientation(RecyclerView.VERTICAL);
            order_pet_recycle.setLayoutManager(manager);
        }
    }

    @NonNull
    @NotNull
    @Override
    public OrderShopAdapter.OrderShopViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.comment_order_shop, parent, false);
        return new OrderShopAdapter.OrderShopViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull OrderShopAdapter.OrderShopViewHolder holder, int position) {
        if(state == 0) {
            holder.order_state.setText("待付款");
        } else if(state == 1) {
            holder.order_state.setText("待发货");
        } else if(state == 2) {
            holder.order_state.setText("已发货");
        } else if(state == 3) {
            holder.order_state.setText("待评价");
        } else if(state == 4) {
            holder.order_state.setText("申请售后");
        }

        if(state > 0 && state < 3) {
            holder.order_sure.setVisibility(View.VISIBLE);
        } else if(state >= 3) {
            holder.order_sure.setVisibility(View.VISIBLE);
            holder.order_sure.setText("已确定");
            holder.order_sure.setEnabled(false);
        }
        OrderPetAdapter adapter = new OrderPetAdapter(petsList.get(position), context);
        holder.order_pet_recycle.setAdapter(adapter);

        ShopRepository.getByShopId(shopId.get(position), new Consumer<Shop>() {
            @Override
            public void accept(Shop shop) {
                holder.order_shop_name.setText(shop.getName());
            }
        });
    }

    @Override
    public int getItemCount() {
        return petsList.size();
    }
}
